package palf.menuItems {
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

import palf.commands.Command;

internal class MenuButton implements MenuItem {
	private var label : String;
	private var command : Command;

	public function MenuButton(label : String, command : Command) {
		this.label = label;
		this.command = command;
	}

	public function draw(canvas : DisplayObjectContainer) : void {
		var sprite : Sprite = drawGraphics();
		var text_field : TextField = createTextField();
		text_field.x = (sprite.width - text_field.width) / 2;
		text_field.y = (sprite.height - text_field.height) / 2;
		sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseClick);
		sprite.addChild(text_field);
		canvas.addChild(sprite);
	}

	private function drawGraphics() : Sprite {
		var sprite : Sprite = new Sprite();
		sprite.graphics.beginFill(0xcccccc);
		sprite.graphics.drawRoundRect(0, 0, 100, 24, 20);
		sprite.graphics.endFill();
		sprite.buttonMode = true;
		return sprite;
	}

	private function createTextField() : TextField {
		var text_field : TextField = new TextField();
		text_field.defaultTextFormat = new TextFormat("Tahoma, _sans", 12, 0x333333);
		text_field.multiline = false;
		text_field.selectable = false;
		text_field.mouseEnabled = false;
		text_field.text = label;
		text_field.autoSize = TextFieldAutoSize.CENTER;
		return text_field;
	}

	private function onMouseClick(event : MouseEvent) : void {
		command.execute();
	}

}
}
